<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tao - ghi va doc file</title>
<style type="text/css">
<!--
.style1 {
	font-family: LucidaHandwriting, LucidaCalligraphy;
	font-size: 20px;
	font-weight: bold;
	color: #FFFFFF;
}
.style6 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: smaller; }
-->
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="Bai4_4.php">
  <table width="350" border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#DB95B8">
    <tr bgcolor="#BDBAE7">
      <td colspan="2" bgcolor="#CC3366"> <div align="center"><span class="style1">TẠO - GHI - ĐỌC FILE CÓ ĐỊNH DẠNG</span></div></td>
    </tr>
    <tr>
      <td><span class="style6">&nbsp;Tên file: </span></td>
      <td><input name="ten_file" type="text" id="ten_file" size="35" value="tap_tin/sp.txt"/></td>
    </tr>
    <tr>
      <td><span class="style6">&nbsp;Tên hàng </span></td>
      <td><label>
        <input name="ten_hang" type="text" id="ten_hang" value="<?php echo $_POST['ten_hang'];?>" size="25" />
      </label></td>
    </tr>
    <tr>
      <td><span class="style6">&nbsp;Số lượng </span></td>
      <td><input name="sl" type="text" id="sl" value="<?php echo $_POST['sl'];?>" size="15" /></td>
    </tr>
    <tr>
      <td width="103"><span class="style6">&nbsp;Đơn giá </span></td>
      <td width="233"><label>
        <input name="dg" type="text" id="dg" value="<?php echo $_POST['dg'];?>" size="15" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="Submit" value="Ghi và đọc file" />      </td>
    </tr>
  </table>
</form>
<?php
	if(isset($_POST['ten_file']))
	{
		$ten_file=$_POST['ten_file'];
		$ten_hang=$_POST['ten_hang'];
		$sl=$_POST['sl'];
		$dg=$_POST['dg'];
		$thanh_tien=$sl*$dg;
		//Ghi file
		$file=fopen($ten_file,"a",1);
		$san_pham=$ten_hang."\t".$sl."\t".$dg."\t".$thanh_tien."\n";
		fwrite($file,$san_pham);
		echo "Ghi file thành công\n";
		fclose($file);
		//Doc file
		$file=fopen($ten_file,"r",1);
		echo "<table width='350' border='0' align='center' cellpadding='2' cellspacing='2' bgcolor='#DB95B8'>";
		echo "<tr><th>Tên hàng</th>";
		echo "<th>Số lượng</th>";
		echo "<th>Đơn giá</th>";
		echo "<th>Thành tiền</th></tr>";
				
		while(!feof($file))
		{
			echo "<tr>";
			$noi_dung=fgets($file);
			$mang=explode("\t",$noi_dung);
			echo "<td>".$mang[0]."</td>";
			echo "<td>".$mang[1]."</td>";
			echo "<td>".$mang[2]."</td>";
			echo "<td>".$mang[3]."</td>";	
			echo "</tr>";
		}
		fclose($file);
		echo "</table>";
		echo "<br />Đã đọc file xong":
	}
	else
		echo "<br />Chưa nhập tên file";
?>
</body>
</html>
